	import java.io.BufferedReader;
	import java.io.BufferedWriter;
	import java.io.File;
	import java.io.FileWriter;
	import java.io.IOException;
	import java.io.InputStream;
	import java.io.InputStreamReader;
	import java.nio.file.Path;
	import java.nio.file.Paths;
	import java.util.HashMap;
	import java.util.HashSet;
	import Tagger.AutoTagAlgorithm;
	public class FilePathAutoTagAlgorithm implements AutoTagAlgorithm {
		private HashMap<String,HashSet<String>> fromKeyWordsToTagsTable;
		public FilePathAutoTagAlgorithm(){
			
			fromKeyWordsToTagsTable=new HashMap<String,HashSet<String>>();
			
			//search for "FilePathAutoTagAlgorithm.txt" file ,to get users definitions for mapping keywords to tags.		
			Path file = Paths.get("FilePathAutoTagAlgorithm.txt"); //here the text file should be.
			
		
			if(file.exists())//case the file "FilePathAutoTagAlgorithm.txt" exists, build attributes from it.
			{
				buildFromFile(file);
			}
			else //case the file not exist. create it with default values, and build attributes from it.
			{
				//create it with default values
				
				BufferedWriter out=null;
				try {
					out = new BufferedWriter(new FileWriter(file.toString())); 
					
					//default values.
					String[] data={"operation systems:windows,linux,solaris" ,
								   "studies:grade,lecture,exercise" 		 ,
								   "family:dad,mom,brother,sister,shmuely"
						 		  };
					int size=data.length;
					for(int i=0 ; i<size ; i++){
						out.write(data[i]);
						out.newLine();
					}
				} 
				catch (IOException x) {
				}
				 //close the file
				finally {
				    if (out != null)
						try {
							out.flush();
							out.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
				}
				
				//build attributes from it.
				buildFromFile(file);
			}
		}
		
		//This method build fromKeyWordsToTagsTable attribute, from a file.  
		public void buildFromFile(Path file ){
			InputStream in = null;
			try {
			    in = file.newInputStream();
			    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			    String line = null;int size;
			    //get a line, break it into peaces, and case syntax is good put it in 'fromKeyWordsToTagsTable' table.
			    while( (line = reader.readLine()) != null) {	
			    	line.toLowerCase();
			    	String[] sepetare=line.split(":");
			    	if(sepetare.length!=2){//case wrong syntax - continue to next row.
			    		System.out.println("worng syntax on FilePathAutoTagAlgorithm.txt file !");
			    		continue;
			    	}
			    	String[] data=sepetare[1].split(",");
			    	
			    	size=data.length;
			    	// for every key word at that row. notice that data[0] contains a tag, and data[2]...data[n] contains keywords. 
			    	for(int i=0;i<size;i++){ 
			    		if(fromKeyWordsToTagsTable.get(data[i])!=null){//case the keyword already exist add the tag (data[0]) to its HashSet.
			    			fromKeyWordsToTagsTable.get(data[i]).add(sepetare[0]);
			    		}
			    		else{//put the keyword in fromKeyWordsToTagsTable with it's tag.(data[0])
			    			HashSet<String> x= new HashSet<String>();
			    			x.add(sepetare[0]);
			    			fromKeyWordsToTagsTable.put(data[i],x);
			    		}
			    	}
			    	
			    }
			    
			} 
			catch (IOException x) {
			}
			 //close the file
			finally {
			    if (in != null)
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
		}
		public String getInfo() {
				return "This Algorithm returns tags \naccordimg to key words in file \npath. you can set the mapping \nfrom key words to tags your \nself, by changing the \n'FilePathAutoTagAlgorithm.txt' file. \nenjoy.";
		}

		public String[] getTagsForFile(File file) {
			HashSet<String> result=new HashSet<String>();//will get all the tags.
			//turn the file to path.
			Path p = file.toPath().toAbsolutePath();
			int size=p.getNameCount();
			// for every key word at the path.
	    	for(int i=0;i<size;i++){ 
	    		if(fromKeyWordsToTagsTable.get(p.getName(i).toString().toLowerCase())!=null)//case the keyword  exist add the tags it is associated with to result.
	    			result.addAll(fromKeyWordsToTagsTable.get(p.getName(i).toString()));
	    	}
	    	//turn 'result' to String[] type.	
	    	result.remove(null);
			String[] a=new String[result.size()];
			return  result.toArray(a);	
		}

}

